<?php
namespace app\models;

use yii;
use yii\db\Query;

class Rebate extends Query
{
    public function insert_rebate($data)
    {
        $key = ['date','factory','product','return_money','return_rate','invocie'];
        foreach ($key as $k => $v) {
            $key[$k] = '`'.$v.'`';
        }
        $key_str = '('.implode(',', $key).')';
        $value_str  = [];
        foreach ($data as $k => $v) {
            foreach ($v as $k2 => $v2) {
                $v[$k2] = '\''.$v2.'\'';
            }
            $value_str[] = '('.implode(',', $v).')';
        }
        $sql = "INSERT INTO ".yii::$app->db->tablePrefix."tax_rebate".$key_str.'values'.implode(',', $value_str);
        return yii::$app->db->createCommand($sql)->execute();
    }

    public function get_rebate_size($where)
    {
        $db = (new yii\db\Query)->select('count(id) as num')
        ->from(yii::$app->db->tablePrefix."tax_rebate");
        $db->where(['>=','date',$where['begin_time']]);
        $db->andwhere(['<=','date',$where['end_time']]);
        return $db->one();
    }

    public function get_rebate_list($p, $s, $where)
    {
        $db = (new yii\db\Query)->select('*')->from(yii::$app->db->tablePrefix."tax_rebate");
        $db->where(['>=','date',$where['begin_time']]);
        $db->andwhere(['<=','date',$where['end_time']]);
        return $db->all();
    }

    public function del_rebate_by_id($id)
    {
        return yii::$app->db->createCommand()
        ->delete(yii::$app->db->tablePrefix."tax_rebate", ['id'=>$id])->execute();
    }
}
